package com.adarsh.f1.struts.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.adarsh.f1.base.BaseModel;
import com.adarsh.f1.biz.impl.TeamServiceImpl;
import com.adarsh.f1.biz.inf.TeamService;
import com.adarsh.f1.entity.Team;
import com.adarsh.f1.struts.form.TeamForm;

public class TeamAction extends DispatchAction {
	TeamService teamService;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		String action = mapping.getParameter();
		
		if(action.equals("edit"))
			return edit(mapping, form,request,response);
		else if(action.equals("showTime"))
			return showTime(mapping, form,request,response);
		else if(action.equals("insert"))
			return insertTeam(mapping, form,request,response);
		else if(action.equals("delete"))
			return deleteTeam(mapping, form,request,response);
		else
			return null;
	}

	private ActionForward deleteTeam(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		TeamForm teamForm = (TeamForm)form;
		String teamId = request.getParameter("teamId");
		if(teamId != null){
			teamService
			Team team = (Team) teamService.find(Team.class, Long.parseLong(teamId));
			
			if()
		}
		
		
		teamForm.setTeamList(getTeam());
		
		return mapping.findForward("continue");
	}

	private ActionForward insertTeam(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		
		TeamForm teamForm = (TeamForm)form;
		Team team = new Team();
		team.setTeamName(teamForm.getTeamName());

		teamService.insert(team);
		
		teamForm.setTeamList(getTeam());
		
		return mapping.findForward("continue");
	}

	private List<BaseModel> getTeam() {
		return teamService.getAllTeam();
	}

	private ActionForward showTime(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		printWriter.append((new Date()).toString());
		printWriter.flush();
		printWriter.close();
		return null;
	}

	private ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		TeamForm teamForm = (TeamForm)form;
		//List<Team> teamList = getTeam();
		//teamForm.setTeamList(teamList);

		return mapping.findForward("continue");
		
	}

	public TeamService getTeamService() {
		return teamService;
	}

	public void setTeamService(TeamService teamService) {
		this.teamService = teamService;
	}
}